from django.db import models
from datetime import date

DocType=['银行流水','出行记录','情况说明','转账记录']
DocType=[[item]*2 for item in DocType]

class Archive(models.Model):
    idx=models.AutoField('序号',primary_key=True)
    title=models.CharField('标题',blank=False,max_length=200)
    date=models.DateField('存档时间',blank=False,default=date.today)
    type=models.CharField('资料类型',blank=False,choices=DocType,max_length=10)
    comment=models.TextField('备注',blank=True)

    class Meta:
        verbose_name='资料档案'
        verbose_name_plural='资料档案'
    
    def __str__(self) -> str:
        return self.title

class ArchiveLinkedFile(models.Model):
    app_name='archive'
    owner=models.ForeignKey(Archive,on_delete=models.CASCADE)
    file=models.FileField('文件',upload_to=f'{app_name}/')

    def __str__(self) -> str:
        return self.file.name.split('/')[-1]
    
    class Meta:
        verbose_name='关联文件'
        verbose_name_plural='关联文件'


class SignedDoc(models.Model):
    idx=models.AutoField('序号',primary_key=True)
    doc_id=models.CharField('编号',blank=False,max_length=200)
    title=models.CharField('标题',blank=False,max_length=200)
    date=models.DateField('签批时间',blank=False,default=date.today)
    comment=models.TextField('备注',blank=True)

    class Meta:
        verbose_name='呈批文件'
        verbose_name_plural='呈批文件'
    
    def __str__(self) -> str:
        return f'[{self.doc_id}]{self.title}'

class SignedDocLinkedFile(models.Model):
    app_name='signed_doc'
    owner=models.ForeignKey(SignedDoc,on_delete=models.CASCADE)
    file=models.FileField('文件',upload_to=f'{app_name}/')
    class Meta:
        verbose_name='关联文件'
        verbose_name_plural='关联文件'

    def __str__(self) -> str:
        return self.file.name.split('/')[-1]
    

SecretLevel={
    'Public':'公开',
    'Inside':'内部文件',
    'Secret':'秘密',
    'Confidential':'机密',
    'TopSecret':'绝密',
}


class PostedDoc(models.Model):
    idx=models.AutoField('序号',primary_key=True)
    title=models.CharField('标题',blank=False,max_length=200)
    doc_id=models.CharField('编号',blank=False,max_length=40)
    classification=models.TextField('密级',choices=SecretLevel,blank=False)
    # sender=models.CharField('发文单位',blank=False,max_length=100)
    sender=models.CharField('发送人',blank=False,max_length=10)
    post_date=models.DateField('发送时间',blank=False,default=date.today)


    def __str__(self) -> str:
        return f'[{self.doc_id}]{self.title}'

    class Meta:
        verbose_name='发文记录'
        verbose_name_plural='发文记录'


class PostedDocLinkedFile(models.Model):
    app_name='posted_doc'
    owner=models.ForeignKey(PostedDoc,on_delete=models.CASCADE)
    file=models.FileField('文件',upload_to=f'{app_name}/')
    class Meta:
        verbose_name='关联文件'
        verbose_name_plural='关联文件'

    def __str__(self) -> str:
        return self.file.name.split('/')[-1]


class ReceivedDoc(models.Model):
    idx=models.AutoField('序号',primary_key=True)
    title=models.CharField('标题',blank=False,max_length=200)
    doc_id=models.CharField('编号',blank=False,max_length=40)
    classification=models.TextField('密级',choices=SecretLevel,blank=False)
    sender=models.CharField('发文单位',blank=False,max_length=100)
    recipient=models.CharField('签收人',blank=False,max_length=10)
    receive_date=models.DateField('签收时间',blank=False,default=date.today)

    def __str__(self) -> str:
        return f'[{self.doc_id}]{self.title}'

    class Meta:
        verbose_name='收文记录'
        verbose_name_plural='收文记录'

class ReceivedDocLinkedFile(models.Model):
    app_name='received_doc'
    owner=models.ForeignKey(ReceivedDoc,on_delete=models.CASCADE)
    file=models.FileField('文件',upload_to=f'{app_name}/')
    class Meta:
        verbose_name='关联文件'
        verbose_name_plural='关联文件'

    def __str__(self) -> str:
        return self.file.name.split('/')[-1]
    

class PhoneRecord(models.Model):
    idx=models.AutoField('序号',primary_key=True)
    title=models.CharField('记录名称',blank=False,max_length=200)
    date=models.DateField('记录时间',blank=False,default=date.today)
    comment=models.TextField('备注',blank=True)

    class Meta:
        verbose_name='电话记录'
        verbose_name_plural='电话记录'
    
    def __str__(self) -> str:
        return self.title

class PhoneRecordLinkedFile(models.Model):
    app_name='phone_record'
    owner=models.ForeignKey(PhoneRecord,on_delete=models.CASCADE)
    file=models.FileField('文件',upload_to=f'{app_name}/')

    class Meta:
        verbose_name='关联文件'
        verbose_name_plural='关联文件'

    def __str__(self) -> str:
        return self.file.name.split('/')[-1]
